/* *************************************************************************** *
* Sierra Leone - Social Incentives for Childhood Immunization                  *
* The Effects of Signals on Timely and Complete Vaccination, Separated by      *
* Treatment  Without bootstrapping                                             *
* **************************************************************************** *

** Purpose:     The Effects of Signals on Timely and Complete Vaccination, 
				Separated by Treatment, Without Bootstrapping
*/
* **************************************************************************** *



* **************************************************************************** *
* 1.) Generate  a data set with the outcomes constructed according to timeliness
* **************************************************************************** *

	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear

	* Drop babies born in December 2017 and January 2018:
	keep if  dob < td(01dec2017)


	* Keep only children that were at least 12 months age the last time we observed them:
	keep if  age_pikin >= 365


	* ----------------------------------------------------------------------------
	* Create a follow-up indicator for each vaccine based on the age of the child
	*   during the last listing:

	local    VaccineVars = " bcg penta1 penta2 penta3 measles1 "

	foreach      vaccine of local VaccineVars {
		gen     age_`vaccine'_mopup    = .

	}


	replace age_bcg_mopup      = 0            if age_listing !=. & age_listing >= 91
	replace age_bcg_mopup      = 1            if age_listing !=. & age_listing <  91

	replace age_penta1_mopup   = 0            if age_listing !=. & age_listing >= 122
	replace age_penta1_mopup   = 1            if age_listing !=. & age_listing <  122

	replace age_penta2_mopup   = 0            if age_listing !=. & age_listing >= 152
	replace age_penta2_mopup   = 1            if age_listing !=. & age_listing <  152

	replace age_penta3_mopup   = 0            if age_listing !=. & age_listing >= 183
	replace age_penta3_mopup   = 1            if age_listing !=. & age_listing <  183

	replace age_measles1_mopup = 0            if age_listing !=. & age_listing >= 350
	replace age_measles1_mopup = 1            if age_listing !=. & age_listing <  350

	gen     age_num_vaccine_mopup = age_measles1_mopup

	summ    age_num_vaccine_mopup if intervention_arm==1, detail
	gen     age_num_vaccine_mopup_dm = age_num_vaccine_mopup - `r(mean)'




	* ------------------------------------------------------------------------------
	* Construct outcomes according to timeliness

	* Save the vacine outcomes in seperate variables that will be used to code
	*      the completeness outcome:
	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
		gen `vaccine'_raw = `vaccine'
	}

	* Code Vaccine 1 outcome
	local a = 91

	foreach var in bcg {
		replace `var' = 0          if `var'==1 & age_`var'>=`a' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`a' & age_pikin!=.
		replace `var' = .          if age_pikin<`a'
	}

	* Code Vaccine 2 outcome
	local b = 122

	foreach var in penta1 {
		replace `var' = 0          if `var'==1 & age_`var'>=`b' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`b' & age_pikin!=.
		replace `var' = .          if age_pikin<`b'

		replace `var' = 0          if bcg ==0 & `var'==1
	}


	* Code Vaccine 3 outcome
	local c = 152

	foreach  var in penta2 {
		replace `var' = 0          if `var'==1 & age_`var'>=`c' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`c' & age_pikin!=.
		replace `var' = .          if age_pikin<`c'

		replace `var' = 0          if penta1 ==0 & `var'==1
	}


	* Code Vaccine 4 outcome
	* 183 days - 6 months

	local d = 183

	foreach  var in penta3 {
		replace `var' = 0          if `var'==1 & age_`var'>=`d' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`d' & age_pikin!=. //is this the problem that people had already come but were not yet 1?
		replace `var' = .          if age_pikin<`d'

		replace `var' = 0          if penta2 ==0 & `var'==1
	}


	* Code Vaccine 5 outcome
	* 350 days - 11.5 months
	local e = 350

	foreach var in measles1 {
		replace `var' = 0          if `var'==1&age_`var'>=`e'&age_`var'!=.
		replace `var' = 0          if `var'==.&age_pikin>=`e'&age_pikin!=.
		replace `var' = .          if age_pikin<`e'

		replace `var' = 0          if penta3 ==0&`var'==1
	}


	* total number of timely vaccines      (num_vaccine)
	gen     num_vaccine = .
	replace num_vaccine = 0          if bcg==0
	replace num_vaccine = 1          if bcg==1      & penta1==0
	replace num_vaccine = 2          if penta1==1   & penta2==0
	replace num_vaccine = 3          if penta2==1   & penta3==0
	replace num_vaccine = 4          if penta3==1   & measles1==0
	replace num_vaccine = 5          if measles1==1

	* ----------------------------------------------------------------------------
	* Demean control variables for this sample:

	* Recall: code categorical, =2 if missing:
	replace good_memory = 2          if good_memory ==.
	tab     good_memory, gen(recall)

	tab     arm_anc, gen(anc)

	foreach var in  recall1 recall2 distance clinic_pop anc2 anc3 anc4 {
		sum  `var' if intervention_arm==1, detail
		gen  `var'_dm    = `var' - `r(mean)'
	}

	summ   bcg penta1 penta2 penta3 measles1

	gen    panel = 1



	* Svae the timely constructed data:
	save  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_timely_analysis.dta", replace


	* **************************************************************************** *
	* 2.) Generate  a data set with the outcomes constructed according to by Age One Year
	* **************************************************************************** *


	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear


	* Keep only children that were at least 12 months age the last time we observed them:
	keep if age_pikin >= 365

	* Drop babies born in December 2017 and January 2018:
	keep if dob < td(01dec2017)



	* ----------------------------------------------------------------------------
	* Create a follow-up indicator for each vaccine based on the age of the child
	*   during the last listing:

	local       VaccineVars = " vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 "

	foreach      vaccine of local VaccineVars {
		gen     age_`vaccine'_mopup    = .
		gen     age_`vaccine'_mopup_dm = .


	}

	replace age_vaccine1_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine1_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine2_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine2_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine3_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine3_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine4_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine4_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine5_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine5_mopup = 1                if age_listing !=. & age_listing <  365

	gen     age_num_vaccine_mopup = age_vaccine5_mopup

	local       VaccineVars = " vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 "


	summ    age_num_vaccine_mopup if intervention_arm==1, detail
	gen     age_num_vaccine_mopup_dm = age_num_vaccine_mopup - `r(mean)'

	* ------------------------------------------------------------------------------
	* Generate Ouctome Variables and Controls:

	* Save the vacine outcomes in seperate variables that will be used to code
	*      the completeness outcome:
	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
		gen    `vaccine'_raw = `vaccine'
	}

	* Code Vaccine 1 outcome
	local a = 365

	foreach  var in bcg penta1 penta2 penta3 measles1 {
		replace `var' = 0          if `var'==1 & age_`var'>=`a' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`a' & age_pikin!=.
		replace `var' = .          if age_pikin<`a'
	}

	* Generate the cumulative number of vaccines:
	local   VaccineVars = " bcg penta1 penta2 penta3 measles1 "
	egen    vaccines    = rowtotal(`VaccineVars')

	foreach vaccine of numlist 1(1)5 {
		gen     vaccine`vaccine' = 0
	}
	foreach vaccine of numlist 1(1)5 {
		replace vaccine`vaccine' = 1          if vaccines >= `vaccine'
	}



	* Code the outcome variable: number of vaccines
	gen     num_vaccine = vaccines


	* --------------------------------------------------------------------------
	* Demean controls:

	tab     arm_anc, gen(anc)

	replace good_memory = 2          if good_memory ==.
	tab     good_memory, gen(recall)


	foreach var in  recall1 recall2 distance clinic_pop anc2 anc3 anc4  {
		sum    `var' if intervention_arm==1, detail
		gen    `var'_dm    = `var' - `r(mean)'
	}

	* --------------------------------------------------------------------------

	gen    panel = 2



	* Svae the timely constructed data:
	save  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_byAgeOneYear_analysis.dta", replace



	* ******************************************************************************
	* Generate Panel with Controls

	use  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_timely_analysis.dta", clear

	append using "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_byAgeOneYear_analysis.dta"


	label var treat3       "Signal at 4"
	label var treat4       "Signal at 5"
	label var treat2       "Uninformative Bracelet"
	label var distance_dm  "Distance"

	summ      bcg penta1 penta2 penta3 measles1   if panel == 1

	summ      bcg penta1 penta2 penta3 measles1   if panel == 2

	local ANC_dm      = " anc2_dm anc3_dm anc4_dm  "
	local Controls_dm = " recall1_dm recall2_dm distance_dm clinic_pop_dm "


	** Panel A: Effects of Signals on Timely Vaccination - Compared to Control:
	eststo clear
	foreach vaccine in bcg penta1 penta2 penta3 measles1 num_vaccine {



		eststo   m_`vaccine'_a:  areg `vaccine' treat3 treat4 treat2  age_`vaccine'_mopup   `Controls_dm'  `ANC_dm'   if panel == 1, absorb(strata) cluster(clinic) robust
		test   treat2 = treat4
		estadd scalar treat2_treat4 = r(p)
		test   treat2 = treat3
		estadd scalar treat2_treat3 = r(p)
		test   treat3 = treat4
		estadd scalar treat3_treat4 = r(p)
		test   treat2 treat3 treat4
		estadd scalar bracelets     = r(p)
		estadd scalar C_mean   = _b[_cons]
		estadd local  Obs      = "`e(N)'"
		estadd local  controls = "Yes"
		eststo m_`vaccine'_a_full
	
	
	}

	esttab  ///
	m_bcg_a_full m_penta1_a_full m_penta2_a_full              ///
	m_penta3_a_full m_measles1_a_full  m_num_vaccine_a_full   ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new_noboot.tex",     ///
	refcat(treat3 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel A:}}} & \multicolumn{6}{c}{Effects of Signals on Timely Vaccination} \\ \hline %", nolabel) ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule"                  ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{1 Vaccine}} & \multicolumn{1}{c}{\textbf{2 Vaccines}} & \multicolumn{1}{c}{\textbf{3 Vaccines}} & \multicolumn{1}{c}{\textbf{4 Vaccines}} & \multicolumn{1}{c}{\textbf{5 Vaccines}} & \multicolumn{1}{c}{\textbf{Total \# of vaccines}} \\")  ///
	keep(treat3 treat4 treat2 distance_dm)                                ///
	scalars("C_mean        Control Group mean"                            ///
	"Obs           Observations"                                  ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"                 ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"                 ///
	"treat3_treat4 p(S4 = S5)"                                    ///
	"bracelets     Joint F-Test"                                  ///
	"controls      Controls")                                     ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                               ///
	fragment nogaps nomtitles label noobs nolines                 ///
	replace


	** Panel B: Effects of Signals on Vaccination by Age One Year
	foreach vaccine in vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 num_vaccine {

		eststo   model_`vaccine'_b:  areg `vaccine' treat3 treat4 treat2   age_`vaccine'_mopup `Controls_dm'  `ANC_dm'   if panel == 2,absorb(strata) cluster(clinic) robust
		test   treat2 = treat4
		estadd scalar treat2_treat4 = r(p)
		test   treat2 = treat3
		estadd scalar treat2_treat3 = r(p)
		test   treat3 = treat4
		estadd scalar treat3_treat4 = r(p)
		test   treat2 treat3 treat4
		estadd scalar bracelets     = r(p)
		estadd scalar C_mean  = _b[_cons]
		estadd local Obs      = "`e(N)'"
		estadd local controls = "Yes"
		eststo m_`vaccine'_b_full
	}

	esttab  ///
	m_vaccine1_b_full m_vaccine2_b_full  m_vaccine3_b_full              ///
	m_vaccine4_b_full  m_vaccine5_b_full m_num_vaccine_b_full           ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new_noboot.tex",        ///
	refcat(treat3 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel B:}}}  & \multicolumn{6}{c}{Effects of Signals on Vaccination by Age One Year} \\ \hline %", nolabel) ///
	keep(treat3 treat4 treat2 distance_dm)                              ///
	scalars("C_mean        Control Group mean"                          ///
	"Obs           Observations"                                ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"               ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"               ///
	"treat3_treat4 p(S4 = S5)"                                  ///
	"bracelets     Joint F-Test"                                ///
	"controls      Controls")                                   ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                     ///
	fragment append nomtitles noobs nonumbers nolines                   ///
	postfoot("\bottomrule \end{tabular}")                               ///
	label

	filefilter "${Replicate_SocialSignals_ATables}/Table_panel_controls_new_noboot.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw_noboot.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_panel_controls_new_noboot.tex"

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw_noboot.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_WithControls_noboot.tex", from("pvalue\BS_str") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw_noboot.tex"


	* ----------------------------------------------------------------------------
	* outsheet p-values, with controls

	esttab  ///
	m_bcg_a_full m_penta1_a_full m_penta2_a_full              ///
	m_penta3_a_full m_measles1_a_full  m_num_vaccine_a_full   ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new_noboot.tex",     ///
	refcat(treat3 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel A:}}} & \multicolumn{6}{c}{Effects of Signals on Timely Vaccination} \\ \hline %", nolabel) ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule"                  ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{1 Vaccine}} & \multicolumn{1}{c}{\textbf{2 Vaccines}} & \multicolumn{1}{c}{\textbf{3 Vaccines}} & \multicolumn{1}{c}{\textbf{4 Vaccines}} & \multicolumn{1}{c}{\textbf{5 Vaccines}} & \multicolumn{1}{c}{\textbf{Total \# of vaccines}} \\")  ///
	keep(treat3 treat4 treat2 distance_dm)                                ///
	scalars("C_mean        Control Group mean"                            ///
	"Obs           Observations"                                  ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"                 ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"                 ///
	"treat3_treat4 p(S4 = S5)"                                    ///
	"bracelets     Joint F-Test"                                  ///
	"controls      Controls")                                     ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                               ///
	fragment nogaps nomtitles label noobs nolines                 ///
	replace

	esttab  ///
	m_vaccine1_b_full m_vaccine2_b_full  m_vaccine3_b_full              ///
	m_vaccine4_b_full  m_vaccine5_b_full m_num_vaccine_b_full           ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new_noboot.tex",        ///
	refcat(treat3 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel B:}}}  & \multicolumn{6}{c}{Effects of Signals on Vaccination by Age One Year} \\ \hline %", nolabel) ///
	keep(treat3 treat4 treat2 distance_dm)                              ///
	scalars("C_mean        Control Group mean"                          ///
	"Obs           Observations"                                ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"               ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"               ///
	"treat3_treat4 p(S4 = S5)"                                  ///
	"bracelets     Joint F-Test"                                ///
	"controls      Controls")                                   ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                                     ///
	fragment append nomtitles noobs nonumbers nolines                   ///
	postfoot("\bottomrule \end{tabular}")                               ///
	label

	filefilter "${Replicate_SocialSignals_ATables}/Table_panel_controls_new_noboot.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw_noboot.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_panel_controls_new_noboot.tex"

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw_noboot.tex"      ///
	"${Replicate_SocialSignals_ATables}/pvalues/Table_EffectsOfSignals_Timely_AgeOneYear_WithControls_pvals_noboot.tex", from("pvalue\BS_str") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw_noboot.tex"






* **************************************************************************** *
*  End of the Dofile !!!
* **************************************************************************** *
